Rust: Debugging and Profiling
Print
debug print
https://doc.rust-lang.org/std/fmt/trait.Debug.html
code: example.rs
#derive(Debug)
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
println!("The origin is: {:?}", origin);
dbg macro
(nightly version only for now)
https://doc.rust-lang.org/std/macro.dbg.html
https://qiita.com/utam0k/items/10915924eb959403d765 (Japanese)
code: example.rs
let a = 2;
let b = dbg!(a * 2) + 1;
// ^-- prints: src/main.rs:2 a * 2 = 4
assert_eq!(b, 5);
Type of variable
https://www.programming-idioms.org/idiom/94/print-type-of-variable/1969/rust
code: example.rs
#!feature(core_intrinsics)
fn type_of<T>(_: &T) -> &'static str {
unsafe { std::intrinsics::type_name::<T>() }
}
println!("{}", type_of(&x));
Logging
debug-assertions
debuginfo
debuginfo-lines
https://qnighy.hatenablog.com/entry/2018/09/17/190000
Backtrace
https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html
RUST_BACKTRACE=1
Debugger
gdb
https://users.rust-lang.org/t/debugging-with-gdb-setting-a-breakpoint-in-main/16830
http://segafreder.hatenablog.com/entry/2018/12/13/145210
LLDB + VSCode
https://github.com/vadimcn/vscode-lldb
Profiling
cargo-profiler
https://github.com/svenstaro/cargo-profiler
https://keens.github.io/blog/2016/05/14/cargo_profilerwotamesu/
#Rust